﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace MagicBox
{
    public class Base64Encoder
    {
        public static void FromBytesToText(string pathFrom, string pathTo)
        {
            string asText;
            using (FileStream fileStream = new FileStream(pathFrom, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader reader = new BinaryReader(fileStream))
                {
                    byte[] asBytes = reader.ReadBytes((int)fileStream.Length);
                    asText = Convert.ToBase64String(asBytes);
                }
            }

            if (string.IsNullOrEmpty(asText)) return;

            using (TextWriter writer = new StreamWriter(pathTo))
            {
                writer.Write(asText);
            }
        }

        public static void FromTextToBytes(string pathFrom, string pathTo)
        {
            byte[] asBytes;
            using(TextReader reader = new StreamReader(pathFrom))
            {
                string asText = reader.ReadToEnd();
                asBytes = Convert.FromBase64String(asText);
            }

            using(FileStream fileStream = new FileStream(pathTo, FileMode.Create, FileAccess.Write))
            {
                using ( BinaryWriter writer = new BinaryWriter(fileStream))
                {
                    writer.Write(asBytes);
                }
            }
        }

    }
}
